import { IKvManager } from './IKvManager';
import distributedKVStore from '@ohos.data.distributedKVStore';
import { ValueType } from '@kit.ArkData';

const TAG: string = '[KvStoreManager]'

export class KvStoreManager implements IKvManager {
  private kvStore: distributedKVStore.SingleKVStore = null;

  putSync(key: string, value: ValueType) {
  }

  getSync(key: string, defValue: ValueType): ValueType {
    return ""
  }

  get(key: string, defValue: ValueType): Promise<any> {
    return new Promise((resolve, reject) => {
      if (this.kvStore == null) {
        reject("kvStore is null");
      } else {
        this.kvStore.get(key).then((data) => {
          console.info(`Succeeded in getting data.data=${data}`);
          resolve(data);
        }).catch((error) => {
          console.error(`Failed to get.code is ${error.code},message is ${error.message}`);
          resolve("");
        });
      }
    });
  }

  put(key: string, value: ValueType): Promise<void> {
    return new Promise((resolve, reject) => {
      try {
        if (this.kvStore == null) {
          reject("kvStore is null");
        } else {
          this.kvStore.put(key, value).then(() => {
            console.info(`Succeeded in putting data.`);
            this.commit()
            resolve();
          }).catch((error) => {
            console.error(`Failed to put.code is ${error.code},message is ${error.message}`);
            reject(error);
          });
        }
      } catch (error) {
        console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`);
        reject(error);
      }
    });
  }

  commit(): Promise<void> {
    return this.kvStore.commit();
  }

  has(key: string): Promise<boolean> {
    return new Promise((resolve, reject) => {
      if (this.kvStore == null) {
        reject("kvStore is null");
      } else {
        this.kvStore.get(key).then((data) => {
          console.info(`kvStore has key ${key}`);
          resolve(true);
        }).catch((error) => {
          console.error(`kvStore has not key ${key}`);
          resolve(false);
        });
      }
    });
  }

  init(context): Promise<void> {
    return new Promise((resolve, reject) => {
      let kvManager;
      const kvManagerConfig = {
        context: context,
        bundleName: 'com.linlong.x.ssa.hm',
      }
      try {
        kvManager = distributedKVStore.createKVManager(kvManagerConfig);
        console.info("Succeeded in creating KVManager");
      } catch (error) {
        console.error(`Failed to create KVManager.code is ${error.code},message is ${error.message}`);
        reject(error);
        return;
      }

      try {
        const options = {
          createIfMissing: true,
          encrypt: false,
          backup: false,
          autoSync: true,
          kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION,
          securityLevel: distributedKVStore.SecurityLevel.S2,
        };
        kvManager.getKVStore('jncj', options).then((store) => {
          console.info("Succeeded in getting KVStore");
          this.kvStore = store;
          resolve();
        }).catch((error) => {
          console.error(`Failed to get KVStore.code is ${error.code},message is ${error.message}`);
          reject(error);
        });
      } catch (error) {
        console.error(`An unexpected error occurred.code is ${error.code},message is ${error.message}`);
        reject(error);
      }
    });
  }
}